/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.RoleModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class RolesComponent extends EditTableComponent
{	
	protected List<RoleModel> roleModels;
	protected RoleModel emptyRoleModel;

	/**
	 * Constructor
	 */
	public RolesComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		emptyRoleModel = new RoleModel();
		roleModels = new ArrayList<RoleModel>();
		paginate = false;
		tableType = "roles";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		roleModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		RoleModel model = new RoleModel(jsonObject);
		roleModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = roleModels.size();
		if (edit)
			numberOfTableColumns = 2;
		else 
			numberOfTableColumns = 1;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Name");
		if (edit) grid.setText(0, 1, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, roleModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyRoleModel);
	}

	public void displayRow(Grid grid, int row, RoleModel model) {
		if (edit) {			
			Hidden idHidden = new Hidden();
			idHidden.setName("roleId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("roleId-" + row, idHidden);
			
			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("160px");
			nameTextBox.setText(model.getName());
			elements.put("roleName-" + row, nameTextBox);
			grid.setWidget(row, 0, nameTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("roleDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 1, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getName());
		}
	}

	public List<RoleModel> getRoleModels() {
		return roleModels;
	}
}
